using System;
using System.Drawing;
using System.Windows.Forms;

using Elev.UI.Controls;
using Elev.Core;

namespace Elev.UI
{
    public partial class ExamTypeForm : Form
    {
        bool nonNumberEntered;
        ExamType examType;

        public ExamType ExamType
        {
            get { return examType; }
            set
            {
                examType = value;
                
                // Pre-fill the form
                nameTextBox.Text = examType.Name;
                coefficientTextBox.Text = examType.Coefficient.ToString();
                colorPickerButton.BackColor = examType.DisplayColor;

                Text = "Editer un type d'examen";
                nameTextBox.SelectAll();
            }
        }

        public ExamTypeForm()
        {
            InitializeComponent();

            Text = "Ajouter un type d'examen";
            coefficientTextBox.Text = "1";

            colorPickerButton.Click += delegate
            {
                colorDialog.Color = colorPickerButton.BackColor;
                if (colorDialog.ShowDialog() == DialogResult.OK)
                {
                    colorPickerButton.BackColor = colorDialog.Color;
                }
            };

            okButton.Click += delegate
            {
                Ok();
            };

            cancelButton.Click += delegate
            {
                Cancel();
            };
        }

        void Ok()
        {
            if (nameTextBox.Text == "")
            {
                ControlsHelper.HighlightMissingField(nameTextBox, "Nom obligatoire!");
                return;
            }

            if (coefficientTextBox.Text == "")
                coefficientTextBox.Text = "1";
            
            if (examType == null)
                examType = new ExamType();

            examType.Name = nameTextBox.Text;
            examType.Coefficient = Convert.ToInt32(coefficientTextBox.Text);
            examType.DisplayColor = colorPickerButton.BackColor;

            try
            {
                examType.Save();
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        void Form_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Return:
                    Ok();
                    break;
                case Keys.Escape:
                    Cancel();
                    break;
            }
        }

        void coefficientTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            nonNumberEntered = false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                    if (e.KeyCode != Keys.Back)
                        nonNumberEntered = true;     
        }

        void coefficientTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (nonNumberEntered == true)
                e.Handled = true;
        }

        void nameTextBox_TextChanged(object sender, EventArgs e)
        {
            ((Control)sender).BackColor = Color.White;
        }
    }
}